<?php
class UserHandler extends HandlerBase {
	/*@var $db DbMySQL*/
	public function handle($op) {
		switch ($op) {
			case 'login':
				$this->login(); break;
			case 'addchat':
				$this->addchat(); break;
			case 'AddUserChat':
				$this->AddUserChat(); break;
			case 'GetChatContent':
				$this->GetChatContent(); break;
			case 'GetUserChatContent':
				$this->GetUserChatContent(); break;
			case 'CloseChat':
				$this->CloseChat(); break;
			case 'GetConsuler':
				$this->GetConsuler(); break;
			default:
				$this->writeResponse(false,'false'); break;
		}
	}
	private function login(){
		$user = null;
		$pwd = null;
		$rem = null;
		$p = $this->requireParams('user','pwd','rem');
		extract($p,EXTR_IF_EXISTS);
		if(strlen($user) == 0) $this->writeResponse(false,'请填写您的用户名。');
		if(strlen($pwd) == 0 )$this->writeResponse(false,'请填写您的密码。');
		$rem = $rem == "true" ? true:false;
		$re = Auth::DoLogin($user,$pwd,$rem,false);
		$this->writeResponse(true,$re);
	}
	private function addchat(){
		@session_start();		
		$user = session_id();
		$content = null;
		$p = $this->requireParams('content');
		extract($p,EXTR_IF_EXISTS);
		$db = getDb();
		$re = $db->execute("INSERT INTO consultion(con_user,con_from,con_content,con_time,con_view) VALUES({0},'USER',{1},NOW(),1)",array($user,$content));
		if($re) $this->writeResponse(true,'true');
		else $this->writeResponse(true,'对不起出错了.');
	}
	private function AddUserChat(){
		$admin_id = $this->requireCurrentUser('admin_id');
		$user = null;
		$content = null;
		$p = $this->requireParams('content','user');
		extract($p,EXTR_IF_EXISTS);
		$db = getDb();
		$re = $db->execute("INSERT INTO consultion(con_user,con_from,con_admin,con_content,con_time,con_view) VALUES({0},'ADMIN',{1},{2},NOW(),0)",array($user,$admin_id,$content));
		if($re){
			$db->execute("UPDATE consultion SET con_admin={0} WHERE con_user={1} AND con_admin IS NULL",array($admin_id,$user));
			$this->writeResponse(true,'true');
		}
		else $this->writeResponse(true,'对不起出错了.');
	}
	private function GetChatContent(){
		@session_start();		
		$user = session_id();
		$db = getDb();
		$re = $db->fetch("SELECT * FROM consultion WHERE con_user={0} ORDER BY con_time ASC",array($user));
		$data = $this->jsonEncode($re);
		$this->writeResponse(true,$data);
	}
	private function  GetUserChatContent(){
		$admin_id = $this->requireCurrentUser('admin_id');
		$user = null;
		$p = $this->requireParams('user');
		extract($p,EXTR_IF_EXISTS);
		$db = getDb();
		$re = $db->fetch("SELECT * FROM consultion WHERE con_user={0} ORDER BY con_time ASC",array($user));
		$data = $this->jsonEncode($re);
		$this->writeResponse(true,$data);
	}
	private function CloseChat(){
		@session_start();		
		$user = session_id();
		$db = getDb();
		$re = $db->execute("UPDATE consultion SET con_view=1 WHERE con_user={0}",array($user));
		$this->writeResponse(true,$re);
	}
	private function GetConsuler(){
		$adminId = $this->requireCurrentUser('admin_id');
		$db = getDb();
		$db->execute("DELETE FROM online WHERE time<".(time() - 120));
		$re = $db->fetch("SELECT DISTINCT a.con_user,(SELECT COUNT(*) FROM `consultion` AS b WHERE b.con_admin IS NULL AND b.con_user=a.con_user) AS havemsg FROM consultion AS a WHERE a.con_user IN (SELECT o_user FROM online WHERE o_type=0) ORDER BY a.con_time DESC");
		$re = $this->jsonEncode($re);
		$this->writeResponse(true,$re);
	}
	
	/*
    private function invitefriend(){
    	$email = null;
    	$name = null;
    	$url = null;
    	$groupId = null;
    	$p = $this->requireParams('email','name','url','groupId');
    	extract($p,EXTR_IF_EXISTS);
    	if(!isset($email) || empty($email)){
    		$this->writeResponse(false,'false','请填写Email地址');
    	}
		if(!ereg("^[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]*)*@[a-zA-Z0-9\-]+([\.][a-zA-Z0-9\-]+)+$",$email)){
    		$this->writeResponse(false,'false','Email地址格式不正确');
		}
    	require_once('../Tool/SendInviteMail.class.php');
    	$param = array();
    	$param['email'] = $email;
    	$param['name'] = $name;
    	$param['url'] = $url;
    	$param['groupId'] = $groupId;
    	$invitemail = new SendInviteMail('file:'.dirname(__FILE__).'/../reg/InviteMail.tpl.html',$param);
    	if($invitemail->send()){
    		$this->writeResponse(true,'true','发送成功！');
    	}else{
    		$this->writeResponse(true,'false','对不起，出错了！');
    	}
    }
    */
}
?>